package com.shareach.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.mongo.BaseCategoryDao;
import com.shareach.domain.BaseCategoryDomain;
import com.shareach.service.BaseCategoryService;
import com.shareach.service.exception.BaseService;
import com.shareach.util.ReflectionUtil;

/**
 * create time：2013-1-30 下午6:16:44
 * 
 * @author retryu
 * @since JDK 1.6
 * @version 1.0 description：
 */
@Service
public class BaseCategoryServiceImpl extends BaseService<BaseCategoryDomain> implements BaseCategoryService {

	@Autowired
	private BaseCategoryDao baseCategoryDao;

	@Override
	public void save(BaseCategoryDomain doamin) {
		doamin.setCreatTime(new Date());
		baseCategoryDao.insert(doamin);
	}

	@Override
	public void del(BaseCategoryDomain domain) {
		if (domain.getCode() == null)
			return;
		Query query = new Query();
		query.addCriteria(new Criteria("_id").is(domain.getCode()));
		baseCategoryDao.delete(query, BaseCategoryDomain.class);
	}

	@Override
	public void update(BaseCategoryDomain domain) {
		if (domain.getCode() == null)
			return;
		Long updateId = domain.getCode();
		//否则会更新出code属性
		domain.setCode(null);

		Update update = parseDomaintoUpdate(domain);

		if (update.getUpdateObject().toMap().size() > 0) {
			baseCategoryDao.update(Criteria.where("_id").is(updateId), update);
		}
	}

	@Override
	public List<BaseCategoryDomain> getList(BaseCategoryDomain domain) {
		Query query = parseDomaintoQuery(domain);
		List<BaseCategoryDomain> baseCategoryDomains = baseCategoryDao.findList(query);
		return baseCategoryDomains;
	}

	@Override
	public BaseCategoryDomain getById(Long id) {
		if (id == null)
			return null;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(id));
		BaseCategoryDomain baseCategoryDomain = baseCategoryDao.findOne(query);
		return baseCategoryDomain;
	}

	@Override
	public Page<BaseCategoryDomain> getPageResult(BaseCategoryDomain domain, BaseQueryOrder order) {
		
		Map<Object, Object> domainMap = ReflectionUtil.parseDomaintoMap(domain);
		Query query = new Query();
		for (Entry<Object, Object> entry : domainMap.entrySet()) {
			if (entry.getKey().equals("name")) {
				query.addCriteria(Criteria.where((String) entry.getKey()).regex((String) entry.getValue()));
			} else if (entry.getKey().equals("updateTime")) {
				continue;
			} else {
				query.addCriteria(Criteria.where((String) entry.getKey()).is(entry.getValue()));
			}
		}
		Page<BaseCategoryDomain> domainPage = baseCategoryDao.getPageResult(query, order);
		return domainPage;
	}

	@Override
	public BaseCategoryDomain findOne(BaseCategoryDomain domain) {
		Query query = parseDomaintoQuery(domain);
		BaseCategoryDomain retDom = baseCategoryDao.findOne(query);
		return retDom;
	}

	@Override
	public void update(BaseCategoryDomain domain, BaseCategoryDomain condition) {

		Query query = parseDomaintoQuery(condition);

		Update update = parseDomaintoUpdate(domain);

		if (update.getUpdateObject().toMap().size() != 0) {
			baseCategoryDao.update(query, update);
		}
	}

}
